Add OstreeRepo option for an out-of-band cache dir
authorAlexander Larsson <alexl@redhat.com>
Mon, 11 Apr 2016 10:43:07 +0000 (12:43 +0200)
committerColin Walters (automation) <walters+githubbot@verbum.org>
Thu, 14 Apr 2016 15:55:08 +0000 (15:55 +0000)
This allows you to have a writable cache dir even for a system-owned
repository.

Closes: #250
Approved by: cgwalters

src/libostree/libostree.sym
src/libostree/ostree-repo-private.h
src/libostree/ostree-repo.c
src/libostree/ostree-repo.h
src/ostree/ot-builtin-pull.c
src/ostree/ot-remote-builtin-refs.c
src/ostree/ot-remote-builtin-summary.c
tests/test-pull-summary-sigs.sh

index ea18632feb6743754f2fb18537c665cd311a78b9..89a1457ec12d8e7976a3c173e1af87ca7b2e2e59 100644 (file)
@@ -335,4 +335,5 @@ global:
         ostree_repo_get_remote_option;
         ostree_repo_get_remote_list_option;
         ostree_repo_get_remote_boolean_option;
+        ostree_repo_set_cache_dir;
 } LIBOSTREE_2016.4;
index 1cf99ead4e7b4ffa11c1ca98c09e08b1f64a3a4c..a2a996932e1f0fdce5f42e949f1bd870a33ead58 100644 (file)
@@ -54,6 +54,7 @@ struct OstreeRepo {
   GFile *tmp_dir;
   int    tmp_dir_fd;
   int    cache_dir_fd;
+  char  *cache_dir;
   GFile *objects_dir;
   GFile *state_dir;
   int objects_dir_fd;
index b8cb242594627d5133b2146a4aac010f62fb435c..495e226174e88e2b58253023a3160f8cebe26525 100644 (file)
@@ -2573,6 +2573,36 @@ ostree_repo_set_disable_fsync (OstreeRepo    *self,
   self->disable_fsync = disable_fsync;
 }
 
+/**
+ * ostree_repo_set_cache_dir:
+ * @self: An #OstreeRepo
+ * @dfd: directory fd
+ * @path: subpath in @dfd
+ *
+ * Set a custom location for the cache directory used for e.g.
+ * per-remote summary caches. Setting this manually is useful when
+ * doing operations on a system repo as a user because you don't have
+ * write permissions in the repo, where the cache is normally stored.
+ */
+gboolean
+ostree_repo_set_cache_dir (OstreeRepo    *self,
+                           int            dfd,
+                           const char    *path,
+                           GCancellable  *cancellable,
+                           GError        **error)
+{
+  int fd;
+
+  if (!glnx_opendirat (dfd, path, TRUE, &fd, error))
+    return FALSE;
+
+  if (self->cache_dir_fd != -1)
+    close (self->cache_dir_fd);
+  self->cache_dir_fd = fd;
+
+  return TRUE;
+}
+
 /**
  * ostree_repo_get_disable_fsync:
  * @self: An #OstreeRepo
index 822014f21c0ce9f8d27bdd4410360c420b711bc8..8a04e8e52f48ac3b18c3a79b43f2989fe1b9acdd 100644 (file)
@@ -63,6 +63,13 @@ _OSTREE_PUBLIC
 void          ostree_repo_set_disable_fsync (OstreeRepo    *self,
                                              gboolean       disable_fsync);
 
+_OSTREE_PUBLIC
+gboolean      ostree_repo_set_cache_dir (OstreeRepo    *self,
+                                         int            dfd,
+                                         const char    *path,
+                                         GCancellable   *cancellable,
+                                         GError        **error);
+
 _OSTREE_PUBLIC
 gboolean      ostree_repo_get_disable_fsync (OstreeRepo    *self);
 
index 8bef63a3536bfe7a8aafbe5d2aa4cbb9dbb9e9a1..734f744035a4fd19e8c3739541e6ce7a5b6497c9 100644 (file)
@@ -35,10 +35,12 @@ static gboolean opt_disable_static_deltas;
 static gboolean opt_require_static_deltas;
 static gboolean opt_untrusted;
 static char* opt_subpath;
+static char* opt_cache_dir;
 static int opt_depth = 0;
  
 static GOptionEntry options[] = {
    { "commit-metadata-only", 0, 0, G_OPTION_ARG_NONE, &opt_commit_only, "Fetch only the commit metadata", NULL },
+   { "cache-dir", 0, 0, G_OPTION_ARG_STRING, &opt_cache_dir, "Use custom cache dir", NULL },
    { "disable-fsync", 0, 0, G_OPTION_ARG_NONE, &opt_disable_fsync, "Do not invoke fsync()", NULL },
    { "disable-static-deltas", 0, 0, G_OPTION_ARG_NONE, &opt_disable_static_deltas, "Do not use static deltas", NULL },
    { "require-static-deltas", 0, 0, G_OPTION_ARG_NONE, &opt_require_static_deltas, "Require static deltas", NULL },
@@ -130,6 +132,12 @@ ostree_builtin_pull (int argc, char **argv, GCancellable *cancellable, GError **
   if (opt_disable_fsync)
     ostree_repo_set_disable_fsync (repo, TRUE);
 
+  if (opt_cache_dir)
+    {
+      if (!ostree_repo_set_cache_dir (repo, AT_FDCWD, opt_cache_dir, cancellable, error))
+        goto out;
+    }
+
   if (opt_mirror)
     pullflags |= OSTREE_REPO_PULL_FLAGS_MIRROR;
 
index d21b19cc82fdeee6b41ab64b12986a6eb5c5c34c..da3bcbb9a7f7e9914e1eae609c9be40b15c53a54 100644 (file)
 #include "ot-main.h"
 #include "ot-remote-builtins.h"
 
+static char* opt_cache_dir;
+
 static GOptionEntry option_entries[] = {
+  { "cache-dir", 0, 0, G_OPTION_ARG_STRING, &opt_cache_dir, "Use custom cache dir", NULL },
 };
 
 gboolean
@@ -49,6 +52,12 @@ ot_remote_builtin_refs (int argc, char **argv, GCancellable *cancellable, GError
       goto out;
     }
 
+  if (opt_cache_dir)
+    {
+      if (!ostree_repo_set_cache_dir (repo, AT_FDCWD, opt_cache_dir, cancellable, error))
+        goto out;
+    }
+
   remote_name = argv[1];
 
   if (!ostree_repo_remote_list_refs (repo, remote_name, &refs, cancellable, error))
index da76017f16821867d40f96d293c32dea6cf72d1b..4659dd4dea411de9d49688cbf1b8af47389256f8 100644 (file)
 
 static gboolean opt_raw;
 
+static char* opt_cache_dir;
+
 static GOptionEntry option_entries[] = {
+  { "cache-dir", 0, 0, G_OPTION_ARG_STRING, &opt_cache_dir, "Use custom cache dir", NULL },
   { "raw", 0, 0, G_OPTION_ARG_NONE, &opt_raw, "Show raw variant data", NULL },
   { NULL }
 };
@@ -59,6 +62,12 @@ ot_remote_builtin_summary (int argc, char **argv, GCancellable *cancellable, GEr
 
   remote_name = argv[1];
 
+  if (opt_cache_dir)
+    {
+      if (!ostree_repo_set_cache_dir (repo, AT_FDCWD, opt_cache_dir, cancellable, error))
+        goto out;
+    }
+
   if (opt_raw)
     flags |= OSTREE_DUMP_RAW;
 
index 40de0a666db69c50da1e33e4aa885a04f46352c0..65822d236776a4cfc5407360513856ae7e95cd62 100755 (executable)
@@ -21,7 +21,7 @@ set -euo pipefail
 
 . $(dirname $0)/libtest.sh
 
-echo "1..6"
+echo "1..7"
 
 COMMIT_SIGN="--gpg-homedir=${TEST_GPG_KEYHOME} --gpg-sign=${TEST_GPG_KEYID_1}"
 setup_fake_remote_repo1 "archive-z2" "${COMMIT_SIGN}"
@@ -91,6 +91,21 @@ assert_has_file repo/tmp/cache/summaries/origin
 assert_has_file repo/tmp/cache/summaries/origin.sig
 echo "ok prune summary cache"
 
+cd ${test_tmpdir}
+repo_reinit
+mkdir cachedir
+${OSTREE} --repo=repo pull --cache-dir=cachedir origin main
+assert_not_has_file repo/tmp/cache/summaries/origin
+assert_not_has_file repo/tmp/cache/summaries/origin.sig
+assert_has_file cachedir/summaries/origin
+assert_has_file cachedir/summaries/origin.sig
+
+rm cachedir/summaries/origin
+${OSTREE} --repo=repo pull --cache-dir=cachedir origin main
+assert_not_has_file repo/tmp/cache/summaries/origin
+assert_has_file cachedir/summaries/origin
+
+echo "ok pull with signed summary and cachedir"
 
 cd ${test_tmpdir}
 repo_reinit